package com.tnsw.facebookautoliker.helper.command;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;

import com.tnsw.coreutils.MyLog;
import com.tnsw.coreutils.database.RecordBase;
import com.tnsw.facebookautoliker.database.DBManager;
import com.tnsw.facebookautoliker.database.FriendsList;
import com.tnsw.facebookautoliker.database.FriendsPost;
import com.tnsw.facebookautoliker.helper.FacebookBGManager;
import com.tnsw.facebookautoliker.helper.FacebookCommandObject;
import com.tnsw.facebookautoliker.helper.GeneralManager;
import com.facebook.HttpMethod;
import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Settings;

public class DoLikeObject extends FacebookCommandObject implements Request.Callback {

	private String objectId;
	private Context context;
	private int primarykey;
	
	private DBManager dbm;
	private GeneralManager gm;
	
	public DoLikeObject(Context context, String objectId, int primarykey) {
		super(context);
		super.tag = "DoLikeObject";
		this.context = context;
		this.objectId = objectId;
		this.primarykey = primarykey;
		
		this.gm = GeneralManager.getInstance();
		this.dbm = DBManager.getInstance(context);
	}
	
	private void doSummary() {
		String sql = "select * from friends_post where primarykey=?";
		ArrayList<RecordBase> a = this.dbm.getRecords(FriendsPost.class.getName(), sql, new String[] { String.valueOf(this.primarykey) }, 0);
		if (a != null) {
			FriendsPost fp = (FriendsPost) a.get(0);
			sql = "select count(*), min(created_time) from friends_post where actor_id=?";
			SQLiteSelect s = new SQLiteSelect(this.context, sql, new String[] {String.valueOf(fp.actor_id)});
			s.setTag("name", "doSummary");
			s.setTag("actor_id", fp.actor_id);
			this.gm.addNewQueue(s, 2);
		}
		
		 
	}
	
	@Override
	protected void handleResult(Response response) {
		super.handleResult(response);
		
		MyLog.d(tag, "handleResult() is being called");
		
		if (response != null) {
			MyLog.d(tag, "handling result of " + response.getRequest().getGraphPath());
			if (response.getError() == null) {
				MyLog.d(tag, "not error");
				ArrayList<RecordBase> a = this.dbm.getRecords(FriendsPost.class.getName(), "select * from friends_post where primarykey=?", new String[] { String.valueOf(this.primarykey)}, 0);
				if (a != null) {
					FriendsPost fp = (FriendsPost) a.get(0);
					
					MyLog.d(tag, "updating records..");
					fp.liked = "Y";
					this.dbm.updateRecordByPrimarykey("friends_post", fp, true);
					this.doSummary();
						
				}
			} else {
				//MyLog.e(tag, "handleResult() - " + response.getError().getException().toString(), response.getError().getException());
			}
		}
		
		this.fbm.updateCommandStatus(this, 200, null);
	}
	
	@Override
	public void execute() {
		super.execute();
		
		MyLog.d(tag, "do like objectId=" + this.objectId);
		
		if (MyLog.isProduction == false) {
			Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
		}
		
		Session session = Session.openActiveSessionFromCache(this.context);
		if (session != null) {
			String path = this.objectId + "/likes";

			Request req = new Request(session, path, null, HttpMethod.POST);
			req.setCallback(this);
			this.standardExecute(req);	
		}
	}

	@Override
	public void onCompleted(Response response) {
		this.handleResult(response);
	}

}
